//
//  XMTitleAndValueView.m
//  XMPayAndRedeemSDK
//
//  Created by Tammy on 16/5/23.
//  Copyright © 2016年 Tammy. All rights reserved.
//

#import "XMTitleAndValueView.h"

#import "XMScreenAdaptation.h"
#import "UIColor+RGB.h"

#define MAX_STARWORDS_LENGTH 1000

@interface XMTitleAndValueView ()

@property (nonatomic, strong) UILabel *titleLabel;
//@property (nonatomic, strong) UITextField *valueTextField;
@property (nonatomic, strong) UIToolbar *customAccessoryView;
@property (nonatomic, strong) UIView * upLine;
@property (nonatomic, strong) UIView * downLine;

@end

@implementation XMTitleAndValueView


- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self setupViews];
    }
    return self;
}

#pragma mark - Notification Method
-(void)textFiledEditChanged:(NSNotification *)obj
{
    UITextField *textField = (UITextField *)obj.object;
    NSString *toBeString = textField.text;
    NSString *lang = [textField.textInputMode primaryLanguage];
    if ([lang isEqualToString:@"zh-Hans"])// 简体中文输入
    {
        //获取高亮部分
        UITextRange *selectedRange = [textField markedTextRange];
        UITextPosition *position = [textField positionFromPosition:selectedRange.start offset:0];
        
        // 没有高亮选择的字，则对已输入的文字进行字数统计和限制
        if (!position)
        {
            if (toBeString.length > self.valueTextNumber)
            {
                textField.text = [toBeString substringToIndex:self.valueTextNumber];
            }
        }
        
    }
    // 中文输入法以外的直接对其统计限制即可，不考虑其他语种情况
    else
    {
        if (toBeString.length > self.valueTextNumber)
        {
            NSRange rangeIndex = [toBeString rangeOfComposedCharacterSequenceAtIndex:self.valueTextNumber];
            if (rangeIndex.length == 1)
            {
                textField.text = [toBeString substringToIndex:self.valueTextNumber];
            }
            else
            {
                NSRange rangeRange = [toBeString rangeOfComposedCharacterSequencesForRange:NSMakeRange(0, self.valueTextNumber)];
                textField.text = [toBeString substringWithRange:rangeRange];
            }
        }
    }
}

- (void)setupViews {
    
    self.valueTextNumber = MAX_STARWORDS_LENGTH;
    
    self.backgroundColor = [UIColor whiteColor];
    CGFloat originX = 15 * [XMScreenAdaptation adapterMultipleByWidth];
    CGFloat titleLabelWidth = 80.f * [XMScreenAdaptation adapterMultipleByWidth];
    self.titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(originX, 0, titleLabelWidth, CGRectGetHeight(self.frame))];
//    self.titleLabel.backgroundColor =  [UIColor whiteColor];
    self.titleLabel.font = [UIFont systemFontOfSize:14 * [XMScreenAdaptation adapterMultipleByWidth]];
    self.titleLabel.textColor = [UIColor colorWithRGB:0x333333];
    self.titleLabel.textAlignment = NSTextAlignmentLeft;
    [self addSubview:self.titleLabel];
    
    originX += titleLabelWidth;
    self.valueTextField = [[UITextField alloc] initWithFrame:CGRectMake(originX, 0, CGRectGetWidth(self.frame) - originX - 10 * [XMScreenAdaptation adapterMultipleByWidth], CGRectGetHeight(self.frame))];
//    self.valueTextField.backgroundColor = [UIColor whiteColor];
    self.valueTextField.textAlignment = NSTextAlignmentLeft;
    self.valueTextField.returnKeyType = UIReturnKeyNext;
    self.valueTextField.rightViewMode = UITextFieldViewModeAlways;
    self.valueTextField.leftViewMode = UITextFieldViewModeAlways;
    self.valueTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
    self.valueTextField.keyboardType = UIKeyboardTypeDecimalPad;
    [self addSubview:self.valueTextField];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(textFiledEditChanged:)
                                                name:@"UITextFieldTextDidChangeNotification" object:self.valueTextField];
    
    self.valueTextField.inputAccessoryView = [self customAccessoryView];
    
    self.valueTextField.font = [UIFont systemFontOfSize:14 * [XMScreenAdaptation adapterMultipleByWidth]];
    self.valueTextField.textColor = [UIColor colorWithRGB:0x333333];
    
    
    self.upLine = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, 0.5)];
    self.upLine.backgroundColor = [UIColor colorWithRGB:0xe0e0e0];
    self.upLine.hidden = YES;
    [self addSubview:self.upLine];
    
    self.downLine = [[UIView alloc] initWithFrame:CGRectMake(0, self.frame.size.height, self.frame.size.width, 0.5)];
    self.downLine.hidden = YES;
    self.downLine.backgroundColor = [UIColor colorWithRGB:0xe0e0e0];
    [self addSubview:self.upLine];
    
    //    self.layer.borderColor = [UIColor colorWithRGB:0xe0e0e0].CGColor;
    //    self.layer.borderWidth = 0.5;
}

- (UIToolbar *)customAccessoryView{
    if (!_customAccessoryView) {
        _customAccessoryView = [[UIToolbar alloc]initWithFrame:(CGRect){0, 0, self.frame.size.width, 40}];
        _customAccessoryView.barTintColor = [UIColor colorWithRGB:0xFAFAFA];
        UIBarButtonItem *space = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
        UIBarButtonItem *finish = [[UIBarButtonItem alloc] initWithTitle:@"完成" style:UIBarButtonItemStyleDone target:self action:@selector(doneAction)];
        [_customAccessoryView setItems:@[space,space,finish]];
    }
    return _customAccessoryView;
}


- (void)doneAction{
    [self.valueTextField resignFirstResponder];
}

- (void)setupData:(NSString *)title value:(NSString *)value placeHolder:(NSString *)placeHolder {
    self.titleLabel.text = title;
    self.valueTextField.placeholder = placeHolder;
    [self.valueTextField setValue:[UIColor colorWithRGB:0xb2b2b2] forKeyPath:@"_placeholderLabel.textColor"];
    self.valueTextField.text = value;
    self.valueTextField.textColor = [UIColor colorWithRGB:0x333333];
}

- (void)setupValue:(NSString *)value {
    self.valueTextField.text = value;
}

- (UITextField *)getValueTextField {
    
    return self.valueTextField;
}

//- (void)layoutSubviews {
//    [super layoutSubviews];
//
//    CGRect valueFrame = self.valueTextField.frame;
//    valueFrame.size.width = self.frame.size.width - self.titleLabel.frame.origin.x - self.titleLabel.frame.size.width - 40 * [XMScreenAdaptation adapterMultipleByWidth];
//    self.valueTextField.frame = valueFrame;
//
//}

- (void)setLineFrom:(int)posiontion{
    
    self.downLine.hidden = NO;
    self.upLine.hidden = NO;
    
    CGFloat oX = 15 * [XMScreenAdaptation adapterMultipleByWidth];
    // first 0 - 15
    
    if (posiontion == 1) {
        
        self.downLine.frame = CGRectMake(oX, self.frame.size.height, self.frame.size.width - oX, 0.5);
        // last 15 - 0
    }else if (posiontion == 2){
        self.upLine.frame = CGRectMake(oX, 0, self.frame.size.width - oX, 0.5);
        
        // middle 15 - 15
    }else if(posiontion == 3){
        self.downLine.frame = CGRectMake(oX, self.frame.size.height, self.frame.size.width - oX, 0.5);
        self.upLine.frame = CGRectMake(oX, 0, self.frame.size.width - oX, 0.5);
    }else{
        // 0 - 0
        
        self.upLine.frame = CGRectMake(0, self.frame.size.height, self.frame.size.width, 0.5);
        self.downLine.frame = CGRectMake(0, self.frame.size.height, self.frame.size.width, 0.5);
    }
    
}

- (void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"UITextFieldTextDidChangeNotification" object:self.valueTextField];
}



/*
 // Only override drawRect: if you perform custom drawing.
 // An empty implementation adversely affects performance during animation.
 - (void)drawRect:(CGRect)rect {
 // Drawing code
 }
 */

@end
